//
//  YNSRuningVC.m
//  HNBaseProject
//
//  Created by TR on 2024/7/22.
//

#import "YNSRuningVC.h"
#import "YNSOutView.h"
#import "YNSResultVC.h"

@interface YNSRuningVC ()

@property(nonatomic,strong)UIImageView*bgImageV;
@property(nonatomic,strong)UIImageView*topImageV;
@property(nonatomic,strong)UIProgressView*proView;
@property(nonatomic,strong)UILabel*tlab;
@property(nonatomic,strong)UILabel*numlab;
@property(nonatomic,strong)UILabel*clab;
@property(nonatomic,strong)UIButton*backBtn;

@property(nonatomic,strong)YNSOutView*outView;
@property(nonatomic,strong)NSTimer*timer;
@property(nonatomic,assign)float num;

@end

@implementation YNSRuningVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUI];
    self.num = 0;
//    [self getprogre];
    WEAKSELF;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        if (weakSelf.num >= 1)
        {
            weakSelf.num = 0;
        }
        else{
            weakSelf.num = weakSelf.num+0.1;
        }
        weakSelf.proView.progress = weakSelf.num;
    }];
}
- (void)setUI{
//    self.bgImageV = [[UIImageView alloc]init];
//    self.bgImageV.image = [UIImage imageNamed:@"zz_bg"];
//    [self.view addSubview:self.bgImageV];
//    [self.bgImageV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.right.bottom.offset(0);
//    }];
//    
//    self.topImageV = [[UIImageView alloc]init];
//    self.topImageV.image = [UIImage imageNamed:@"zz_zq"];
//    [self.view addSubview:self.topImageV];
//    [self.topImageV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.offset(176);
//        make.centerX.equalTo(self.view.mas_centerX).offset(0);
//        make.width.height.offset(104);
//    }];
    
    self.tlab = [[UILabel alloc]init];
    self.tlab.text = NSLocalizedString(@"驯服AI中。。。", nil);// @"驯服AI中。。。";
    self.tlab.textColor = [UIColor blackColor];
    self.tlab.font =PFRFontMedium(14);
    [self.view addSubview:self.tlab];
    [self.tlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kNavBarHeight+kRealValue(200));
        make.left.offset(45);
    }];
    
    self.proView = [[UIProgressView alloc]init];
    self.proView.trackTintColor = [UIColor colorWithHexString:@"#E9EBEC"];
    self.proView.tintColor = [UIColor colorWithHexString:@"#00B0F0"];
    self.proView.progress = 0;
    [self.view addSubview:self.proView];
    [self.proView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tlab.mas_bottom).offset(10);
        make.left.offset(45);
        make.right.offset(-45);
        make.height.offset(5);
    }];
    
    
//    self.numlab = [[UILabel alloc]init];
//    self.numlab.text = @"48%";
//    self.numlab.textColor = [UIColor colorWithHexString:@"#FFE14A"];
//    self.numlab.font =PFRFontMedium(28);
//    [self.view addSubview:self.numlab];
//    [self.numlab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.tlab.mas_centerY).offset(0);
//        make.centerX.equalTo(self.proView.centerX).offset(40);
//    }];
    self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backBtn setTitle:NSLocalizedString(@"返回", nil) forState:UIControlStateNormal];
    [self.backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.backBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.backBtn.layer.masksToBounds = YES;
    self.backBtn.layer.cornerRadius = 17.5;
    self.backBtn.backgroundColor = RGBA_COLOR(0, 0, 0, 1);
    [self.backBtn addTarget:self action:@selector(backClik) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.backBtn];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.proView.mas_bottom).offset(83);
        make.centerX.equalTo(self.view);
        make.width.offset(80);
        make.height.offset(35);
    }];
    
    self.clab = [[UILabel alloc]init];
    self.clab.text = NSLocalizedString(@"返回不会影响进度，请到我的制作中查看", nil);// @"";
    self.clab.textColor = [UIColor colorWithHexString:@"#999999"];
    self.clab.font =PFRFont(11);
    self.clab.textAlignment=NSTextAlignmentCenter;
    self.clab.numberOfLines = 0;
    [self.view addSubview:self.clab];
    [self.clab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backBtn.mas_bottom).offset(22);
        make.left.offset(45);
        make.right.offset(-45);
    }];
    
}

#pragma mark-----懒加载------
- (YNSOutView*)outView{
    if (!_outView)
    {
        _outView = [[YNSOutView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    }
    return _outView;
}

#pragma mark------返回-----
-(void)backClik {
//    [self.view addSubview:self.outView];
//    WEAKSELF;
//    self.outView.comfireblock = ^{
//        if (weakSelf.sureLeave) {
//            weakSelf.sureLeave();
//        }
//        [weakSelf.navigationController popViewControllerAnimated:YES];
//    };
    if (self.sureLeave) {
        self.sureLeave();
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark----获取进度------
-(void)getprogre{
    WEAKSELF;
    [[DCHttpClient shareClient] requestWithPath:@"api/video.video/checkTask" params:@{@"video_id":self.video_id,} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * dict = [responseObject mj_JSONObject];
        if([dict[@"code"] intValue]==1){
            if ([[NSString stringWithFormat:@"%@",dict[@"data"][@"state"]] isEqualToString:@"SUCCESS"])
            {
                [weakSelf.timer invalidate];
                weakSelf.timer = nil;
                YNSResultVC *vc= [[YNSResultVC alloc]init];
                vc.cover_image_url = [NSString stringWithFormat:@"%@",dict[@"data"][@"image_url"]];
                vc.video_url = [NSString stringWithFormat:@"%@",dict[@"data"][@"video_url"]];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            else if ([[NSString stringWithFormat:@"%@",dict[@"data"][@"state"]] isEqualToString:@"FAIL"]){
                [weakSelf.timer invalidate];
                weakSelf.timer = nil;
                [SVProgressHUD showInfoWithStatus:@"生成失败"];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
            else{
                [weakSelf performSelector:@selector(getprogre) afterDelay:15];
            }
        }
        else {
            [weakSelf.timer invalidate];
            weakSelf.timer = nil;
            [SVProgressHUD showInfoWithStatus:dict[@"msg"]];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.timer invalidate];
        weakSelf.timer = nil;
        [SVProgressHUD dismiss];
    }];
}
@end
